package com.cykj.pos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cykj.common.annotation.DataSource;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.enums.DataSourceType;
import com.cykj.pos.domain.BizMerchantEntry;
import com.cykj.pos.domain.BizMerchantInfo;
import com.cykj.pos.domain.BizPosMachine;
import com.cykj.pos.domain.BizPosMachineStatusRecords;
import com.cykj.pos.domain.dto.PosMerchantRelatedDTO;
import com.cykj.pos.domain.dto.TransactionDetailsDTO;
import com.cykj.pos.mapper.BizMerchantInfoMapper;
import com.cykj.pos.mapper.BizPosMachineMapper;
import com.cykj.pos.mapper.BizPosMachineStatusRecordsMapper;
import com.cykj.pos.service.IBizMerchantEntryService;
import com.cykj.pos.service.IBizMerchantInfoService;
import com.cykj.pos.service.IBizPosMachineStatusRecordsService;
import com.cykj.pos.util.DateUtils;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 终端设备状态记录Service业务层处理
 *
 * @author weijianbo
 * @date 2021-02-02
 */
@Service
public class BizPosMachineStatusRecordsServiceImpl extends ServiceImpl<BizPosMachineStatusRecordsMapper, BizPosMachineStatusRecords> implements IBizPosMachineStatusRecordsService {
    @Resource
    IBizMerchantEntryService bizMerchantEntryService;
    @Resource
    BizPosMachineStatusRecordsMapper machineStatusRecordsMapper;
    @Resource
    BizPosMachineMapper bizPosMachineMapper;
    @Resource
    BizMerchantInfoMapper bizMerchantInfoMapper;
    @Resource
    IBizMerchantInfoService bizMerchantInfoService;
    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<BizPosMachineStatusRecords> queryList(BizPosMachineStatusRecords bizPosMachineStatusRecords) {
        List<BizPosMachineStatusRecords> list = machineStatusRecordsMapper.list(bizPosMachineStatusRecords);
        return list;
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public BizPosMachineStatusRecords getPosMachineStatusRecordsByMerchantId(String merchantId) {
        LambdaQueryWrapper<BizPosMachineStatusRecords> merchantQuery = Wrappers.lambdaQuery();
        merchantQuery.eq(BizPosMachineStatusRecords::getMerchantId, merchantId);
        BizPosMachineStatusRecords posMachineStatusRecords = this.getOne(merchantQuery);
        return posMachineStatusRecords;
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public BizPosMachineStatusRecords selectBizPosMachineStatusRecords(String snCode) {
        LambdaQueryWrapper<BizPosMachineStatusRecords> mapper = Wrappers.lambdaQuery();
        mapper.eq(BizPosMachineStatusRecords::getSnCode, snCode);
        BizPosMachineStatusRecords posMachineStatusRecords = this.getOne(mapper);
        return posMachineStatusRecords;
    }

    @Override
    @Transactional
    public void updateMachineStatusRecords(BizPosMachineStatusRecords biz) {
        LambdaUpdateWrapper<BizPosMachineStatusRecords> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(BizPosMachineStatusRecords::getSnCode, biz.getSnCode());
        // 改变状态信息 2 已绑定
        biz.setStatus("2");
        machineStatusRecordsMapper.update(biz, wrapper);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public BizPosMachineStatusRecords getPosMachineBindActionRecordsBySnCode(String snCode, String status) {
        LambdaQueryWrapper<BizPosMachineStatusRecords> mapper = Wrappers.lambdaQuery();
        mapper.eq(BizPosMachineStatusRecords::getSnCode, snCode);
        mapper.eq(BizPosMachineStatusRecords::getRecordsType, status);
        return this.getOne(mapper);
    }

    @Override
    public BizPosMachineStatusRecords getPosMachineBindActionRecordsBySnCode(String snCode) {
        LambdaQueryWrapper<BizPosMachineStatusRecords> mapper = Wrappers.lambdaQuery();
        mapper.eq(BizPosMachineStatusRecords::getSnCode, snCode);
        return this.getOne(mapper);
    }

    @Override
    @Transactional
    public void generatePosMerchantRelatedProcedure(String posCode, Long merchId) {
        PosMerchantRelatedDTO posMerchantRelatedDTO = new PosMerchantRelatedDTO();
        posMerchantRelatedDTO.setMerchId(merchId);
        posMerchantRelatedDTO.setPosCode(posCode);
        machineStatusRecordsMapper.generatePosMerchantRelatedProcedure(posMerchantRelatedDTO);
    }

    @Override
    public BizPosMachineStatusRecords queryPosInfoBySncodeOrPhone(BizPosMachineStatusRecords bizPosMachineStatusRecords) {
        BizPosMachineStatusRecords biz = null;
        if (bizPosMachineStatusRecords.getSnCode() != null && !bizPosMachineStatusRecords.getSnCode().equals("")) {
            LambdaQueryWrapper<BizPosMachineStatusRecords> mapper = Wrappers.lambdaQuery();
            mapper.eq(BizPosMachineStatusRecords::getSnCode, bizPosMachineStatusRecords.getSnCode());
            biz = this.getOne(mapper);
        }
        if (bizPosMachineStatusRecords.getPhoneNo() != null && !bizPosMachineStatusRecords.getPhoneNo().equals("")) {
            LambdaQueryWrapper<BizPosMachineStatusRecords> mapper = Wrappers.lambdaQuery();
            mapper.eq(BizPosMachineStatusRecords::getPhoneNo, bizPosMachineStatusRecords.getPhoneNo());
            biz = this.getOne(mapper);
        }
        return biz;
    }

    @Override
    public List<BizPosMachineStatusRecords> queryListActiv(BizPosMachineStatusRecords bizPosMachineStatusRecords) {
        List<BizPosMachineStatusRecords> queryListActiv = machineStatusRecordsMapper.queryListActiv(bizPosMachineStatusRecords);
        return queryListActiv;
    }

    /**
     * pos1.0 设备绑定
     *
     * @param bizMerchantEntry
     * @return
     */
    @Override
    public int snRecordsOneVersion(BizMerchantEntry bizMerchantEntry) {
        String sn = bizMerchantEntry.getSn();
        int row = 0;
        LambdaQueryWrapper<BizPosMachineStatusRecords> merchantQuery = Wrappers.lambdaQuery();
        merchantQuery.eq(BizPosMachineStatusRecords::getSnCode, sn);
        BizPosMachineStatusRecords posMachineStatusRecords = this.getOne(merchantQuery);
        if (posMachineStatusRecords != null) {
            // 已经存在激活信息
//            posMachineStatusRecords.setActiveTime(DateUtils.dateFormatString(new Date()));// 报件时间
            posMachineStatusRecords.setIdCardNo(bizMerchantEntry.getCertificateno());// 身份证号
            posMachineStatusRecords.setPhoneNo(bizMerchantEntry.getPhone());// 电话号
            //records.setBankCardno()
            posMachineStatusRecords.setName(bizMerchantEntry.getApplicant());// 姓名
            posMachineStatusRecords.setSnCode(bizMerchantEntry.getSn());// 设备号
            posMachineStatusRecords.setReceiptDate(DateUtils.dateFormatString(new Date()));
            posMachineStatusRecords.setStatus("2");// 绑定
            posMachineStatusRecords.setRecordsType("1");// 未激活
            machineStatusRecordsMapper.update(posMachineStatusRecords, merchantQuery);
        } else {
            BizPosMachineStatusRecords records = new BizPosMachineStatusRecords();
            if (null != bizMerchantEntry.getMerchantNo()) {
                records.setMerchantId(bizMerchantEntry.getMerchantNo());// 商户编号
            }
            records.setActiveTime(DateUtils.dateFormatString(new Date()));// 报件时间
            records.setIdCardNo(bizMerchantEntry.getCertificateno());// 身份证号
            records.setPhoneNo(bizMerchantEntry.getPhone());// 电话号
            //records.setBankCardno()
            records.setName(bizMerchantEntry.getApplicant());// 姓名
            records.setSnCode(bizMerchantEntry.getSn());// 设备号
            records.setReceiptDate(DateUtils.dateFormatString(new Date()));
            records.setStatus("2");// 绑定
            records.setRecordsType("1");// 未激活
            row = machineStatusRecordsMapper.insert(records);
        }
        return row;
    }

    /**
     * pos1.0设备激活
     *
     * @param snCode
     */
    @Override
    public int disposeOneVersionRecords(String snCode) {
        Long merchId =0l;
        // 更改审核状态 有交易记录代表审核已经成功
        BizMerchantEntry bizMerchantEntry = bizMerchantEntryService.getBizMerchantEntryBySn(snCode);
        BizMerchantInfo bizMerchantInfo=bizMerchantInfoMapper.selectMerchantInfoBySn(snCode);
        if(bizMerchantEntry!=null){
            merchId=bizMerchantEntry.getMerchId();
            if (bizMerchantEntry.getIsAudit().equals("0")) {
                bizMerchantEntry.setIsAudit("3");// 审核成功
                bizMerchantEntryService.saveOrUpdate(bizMerchantEntry);
            }
        }else{
            if(bizMerchantInfo!=null){
                merchId=bizMerchantInfo.getMerchId();
                if(!"3".equals(bizMerchantInfo.getAuditStatus())){
                    bizMerchantInfo.setAuditStatus("3");
                    bizMerchantInfoService.saveOrUpdate(bizMerchantInfo);
                }
            }else{
                return 1;
            }
        }
        LambdaQueryWrapper<BizPosMachineStatusRecords> le = Wrappers.lambdaQuery();
        le.eq(BizPosMachineStatusRecords::getSnCode, snCode);
        BizPosMachineStatusRecords record = this.getOne(le);
        if (record.getRecordsType().equals("2")) {
            // 代表已经激活
        } else {
            // 没有激活 激活一下 改两张表
            // 这个时候运行一下函数
            PosMerchantRelatedDTO posMerchantRelatedDTO = new PosMerchantRelatedDTO();
            posMerchantRelatedDTO.setMerchId(merchId);
            posMerchantRelatedDTO.setPosCode(snCode);
            machineStatusRecordsMapper.generatePosMerchantRelatedProcedure(posMerchantRelatedDTO);// 运行下函数
            // 更新下激活时间
            BizPosMachineStatusRecords bizPosMachineStatusRecords = new BizPosMachineStatusRecords();
            bizPosMachineStatusRecords.setSnCode(snCode);
            bizPosMachineStatusRecords.setActiveTime(DateUtils.dateFormatString(new Date()));
            int time = machineStatusRecordsMapper.updateRecordsAcativation(bizPosMachineStatusRecords);// 更新下激活时间
            int row = machineStatusRecordsMapper.updatePosOneRecords(snCode);
            int pos = bizPosMachineMapper.updatePosOneRecords(snCode);
        }
        return 0 ;
    }

    /**
     * 激活pos
     *
     * @param sn
     * @return
     */
    @Override
    public int updateRecordsService(String sn) {
        PosMerchantRelatedDTO posMerchantRelatedDTO = new PosMerchantRelatedDTO();
        BizPosMachine bizPosMachine = bizPosMachineMapper.selectPosMachineBySn(sn);
        posMerchantRelatedDTO.setMerchId(bizPosMachine.getMerchId());
        posMerchantRelatedDTO.setPosCode(sn);
        String status = machineStatusRecordsMapper.findStatus(sn);
        if (status.equals("1")) {
            machineStatusRecordsMapper.generatePosMerchantRelatedProcedure(posMerchantRelatedDTO);// 运行函数
        }
        int row = machineStatusRecordsMapper.updatePosOneRecords(sn);
        int pos = bizPosMachineMapper.updatePosOneRecords(sn);
        return row + pos;
    }

    @Override
    public void updateRecordsStatus(String snCode) {
        machineStatusRecordsMapper.updateRecordsStatus(snCode);
    }

    @Override
    public void updatePosRecordsStatus(String snCode) {
        machineStatusRecordsMapper.updatePosRecordsStatus(snCode);
    }

    @Override
    public BizPosMachineStatusRecords selectBizPosMachineStatusRecordsBysnCode(String snCode) {
        return machineStatusRecordsMapper.selectBizPosMachineStatusRecordsBysnCode(snCode);
    }

    @Override
    public void insertBizPosRecordsNew(BizPosMachineStatusRecords bizPosMachineStatusRecords) {
        bizPosMachineStatusRecords.setReceiptDate(DateUtils.dateFormatString(new Date()));
        machineStatusRecordsMapper.insertBizPosRecordsNew(bizPosMachineStatusRecords);
    }

    @Override
    public void deleteBizPosRecordsNew(String snCode) {
        machineStatusRecordsMapper.deleteBizPosRecordsNew(snCode);
    }

    @Override
    public void deletebizentrySn(String snCode) {
        machineStatusRecordsMapper.deletebizentrySn(snCode);
    }

    @Override
    public void insertBizPosMachineStatusRecords(BizPosMachineStatusRecords bizPosMachineStatusRecords1) {
        machineStatusRecordsMapper.insertBizPosMachineStatusRecords(bizPosMachineStatusRecords1);
    }

    @Override
    public void updateMachineStatusRecordsByXXL(BizPosMachineStatusRecords bizPosMachineStatusRecords) {
        machineStatusRecordsMapper.updateMachineStatusRecordsByXXL(bizPosMachineStatusRecords);
    }

    @Override
    public int selectActiveCount(TransactionDetailsDTO detailsDTO) {
        return machineStatusRecordsMapper.selectActiveCount(detailsDTO);
    }
}
